/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author wonka
 */
public class MyConnection {
    private String hostName;
    private String databaseName;
    private String userName;
    private String password;
    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;
    
    public MyConnection(String hostName, String databaseName, String userName, String password){
        this.hostName = hostName;
        this.databaseName = databaseName;
        this.userName = userName;
        this.password = password;
    }
    public void testDriver() throws Exception{
       try {
            //Kiểm tra Class Name.
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        }
        // Nếu chưa tồng tại thì mém lỗi ra ngoài.
        catch (java.lang.ClassNotFoundException e) 
        {
                throw new Exception("Không tìm thấy Driver MSSQL JDBC...");
        } 
    }
    
    /**
     * @return the hostName
     */
    public String getHostName() {
        return hostName;
    }

    /**
     * @param hostName the hostName to set
     */
    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    /**
     * @return the databaseName
     */
    public String getDatabaseName() {
        return databaseName;
    }

    /**
     * @param databaseName the databaseName to set
     */
    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the connection
     */
    public Connection getConnection() throws Exception {
        if(this.connection == null){
            testDriver();
        }
        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setUser(this.userName);
        ds.setPassword(this.password);
        ds.setServerName(this.hostName);
        ds.setPortNumber(1433);
        ds.setDatabaseName(this.databaseName);
         this.connection = ds.getConnection();
          return this.connection;
    }
    
    public boolean testConnection(){
        try {
            this.getConnection();
            return true;
        } catch (Exception ex) {
            Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    //Thực thi câu lệnh Select
    public ResultSet executeQuery(String query){
        
        try {
            this.resultSet = getStatement().executeQuery(query);
          
        } catch (Exception ex) {
            Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
          return this.resultSet;
    }
    
    //Thực thi câu lệnh Insert, Update, Delete
    public int executeUpdate(String query) throws SQLException{
        int res = Integer.MIN_VALUE;
        try {
                res = getStatement().executeUpdate(query);
        } catch (Exception ex) {
            Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        closeConnection();
        return res;
    }
    /**
     * @param connection the connection to set
     */
    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    /**
     * @return the statement
     */
    public Statement getStatement() throws  Exception {
        if( this.statement == null?true: this.statement.isClosed())
        {
            this.statement = this.getConnection().createStatement();
        }
        return this.statement;
    }

    /**
     * @param statement the statement to set
     */
    public void setStatement(Statement statement) {
        this.statement = statement;
    }

    /**
     * @return the resultSet
     */
    public ResultSet getResultSet() {
        
        return resultSet;
    }

    /**
     * @param resultSet the resultSet to set
     */
    public void setResultSet(ResultSet resultSet) {
        this.resultSet = resultSet;
    }

    public  void closeConnection() throws SQLException {
        if(this.resultSet != null && !this.resultSet.isClosed()){
                this.resultSet.close();
                this.resultSet = null;
        }
        
         if(this.connection != null && !this.connection.isClosed()){
                this.connection.close();    
                this.connection = null;
        }
    }
    
    
}
